//
//  ServerDetailController.m
//  jboss-admin
//
//  Created by Christos Vasilakis on 22/06/2010.
//  Copyright 2010 All rights reserved.
//

#import "ServerDetailController.h"
#import "ServersViewController.h"
#import "JBossServer.h"

@implementation ServerDetailController

@synthesize server;
@synthesize fieldLabels;
@synthesize tempValues;
@synthesize textFieldBeingEdited;
@synthesize list;

- (void)viewDidLoad {
	[super viewDidLoad];
	
	NSArray *array = [[NSArray alloc] initWithObjects:@"Name", @"Hostname", @"Username", @"Password", nil];
	self.fieldLabels = array;
	
	[array release];
	
	UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancel:)];
	self.navigationItem.leftBarButtonItem = cancelButton;
	[cancelButton release];
	
	UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(save:)];
	self.navigationItem.rightBarButtonItem = saveButton;
	[saveButton release];
	
	NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
	self.tempValues = dict;
	[dict release];
	
	NSLog(@"ServerDetailController viewDidLoad");
}

- (void)viewDidUnload {
	[super viewDidUnload];
	
	self.server = nil;
	self.fieldLabels = nil;
	self.tempValues = nil;
	self.textFieldBeingEdited = nil;
	self.list = nil;

	NSLog(@"ServerDetailController viewDidUnload");
}

- (void)dealloc {
	[server release];
	[fieldLabels release];
	[tempValues release];
	[textFieldBeingEdited release];
	[list release];
	
	NSLog(@"ServerDetailController deAlloc");
		  
	[super dealloc];
}

#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return kNumberOfEditableRows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *ServerCellIdentifier = @"ServerCellIdentifer";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ServerCellIdentifier];
	
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ServerCellIdentifier] autorelease];

		UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 80, 25)];
		label.tag = kLabelTag;
		label.textAlignment = UITextAlignmentRight;
		label.font = [UIFont boldSystemFontOfSize:12.0];
		label.textColor = kNonEditableTextColor;
		
		[cell.contentView addSubview:label];
		[label release];
		
		UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 10, 190, 25)];
		textField.clearsOnBeginEditing = NO;
		[textField setDelegate:self];
		//textField.returnKeyType = UIReturnKeyDone;
		[textField addTarget:self action:@selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEndOnExit];
		
		[cell.contentView addSubview:textField];
	}
	
	NSUInteger row = [indexPath row];
	
	UILabel *label = (UILabel *) [cell viewWithTag:kLabelTag];
	UITextField *textField;
	
	for (UIView *oneView in cell.contentView.subviews) {
		if ([oneView isMemberOfClass:[UITextField class]])
			textField = (UITextField *) oneView;
	}
	
	label.text = [fieldLabels objectAtIndex:row];
	NSNumber *rowAsNum = [[NSNumber alloc] initWithInt:row];
	
	switch (row) {
		case kJBossServerNameRowIndex:
			if ([[tempValues allKeys] containsObject:rowAsNum])
				textField.text = [tempValues objectForKey:rowAsNum];
			else
				textField.text = server.name;
			
			break;
		case kJBossServerHostnameRowIndex:
			if ([[tempValues allKeys] containsObject:rowAsNum])
				textField.text = [tempValues objectForKey:rowAsNum];
			else
				textField.text = server.hostname;
			
			break;
		case kJBossServerUsernameRowIndex:
			if ([[tempValues allKeys] containsObject:rowAsNum])
				textField.text = [tempValues objectForKey:rowAsNum];
			else
				textField.text = server.username;
			
			break;
		case kJBossServerPasswordRowIndex:
			if ([[tempValues allKeys] containsObject:rowAsNum])
				textField.text = [tempValues objectForKey:rowAsNum];
			else
				textField.text = server.password;
			
			break;

		default:
			break;
	}
	
	if (textFieldBeingEdited == textField)
		textFieldBeingEdited = nil;
	
	textField.tag = row;
	[rowAsNum release];
	
	return cell;
}

#pragma mark -
#pragma mark UITextFieldDelegate methods
- (void)textFieldDidBeginEditing:(UITextField *)textField {
	self.textFieldBeingEdited = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
	NSNumber *tagAsNum = [[NSNumber alloc] initWithInt:textField.tag];
	[tempValues setObject:textField.text forKey:tagAsNum];
	[tagAsNum release];
}

#pragma mark -
#pragma mark Action Calls
- (IBAction)cancel:(id)sender {
	[self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)save:(id)sender {
	if (textFieldBeingEdited != nil) {
		NSNumber *tagAsNum = [[NSNumber alloc] initWithInt:textFieldBeingEdited.tag];
		[tempValues setObject:textFieldBeingEdited.text forKey:tagAsNum];
		[tagAsNum release];
	}
	
	for (NSNumber *key in [tempValues allKeys]) {
		switch ([key intValue]) {
			case kJBossServerNameRowIndex:
				server.name = [tempValues objectForKey:key];
				break;
			case kJBossServerHostnameRowIndex:
				server.hostname = [tempValues objectForKey:key];
				break;
			case kJBossServerUsernameRowIndex:
				server.username = [tempValues objectForKey:key];
				break;
			case kJBossServerPasswordRowIndex:
				server.password = [tempValues objectForKey:key];
				break;
			default:
				break;
		}
	}
	
	// if list != nil then we are in ADD mode
	// otherwise don't do anything cause
	// the jboss server instance is changed
	// and will be handled by the parent.reloadData.
	if (list != nil) {
		[list addObject:server];
	}
	
	[self.navigationController popViewControllerAnimated:YES];
}

- (void)textFieldDone:(id)sender {
	UITableViewCell *cell = (UITableViewCell *)[[sender superview] superview];
	UITableView *table = (UITableView *)[cell superview];
	NSIndexPath *textFieldIndexPath = [table indexPathForCell:cell];
	NSUInteger row = [textFieldIndexPath row];
	
	row++;
	if (row >= kNumberOfEditableRows)
		row = 0;
	
	NSUInteger newIndex[] = {0, row};
	NSIndexPath *newPath = [[NSIndexPath alloc] initWithIndexes:newIndex length:2];
	UITableViewCell *nextCell = [self.tableView cellForRowAtIndexPath:newPath];
	[newPath release];
	
	UITextField *nextField = nil;
	
	for (UIView *oneView in nextCell.contentView.subviews) {
		if ([oneView isMemberOfClass:[UITextField class]])
			nextField = (UITextField *)oneView;
	}
	
	[nextField becomeFirstResponder];
}
@end
